﻿/*
 * WARNING:
 * 
 * THIS CODE IS NOT OPEN-SOURCE BUT PROVIDED FOR EDUCATIONAL USE ONLY. 
 * IN FUTURE, THE CODE LICENSE MIGHT BE RELAXED...
 * 
 * FOR MORE INFORMATION ABOUT LICENSING AND HOW YOU CAN JOIN OUR TEAM
 * OR CONTRIBUTE CODE, PLEASE VISIT: http://communitysettlers.codeplex.com/
 * 
 * All rights reserved!
 * Copyright (C) 2010-2011 by Christoph Husse
 */
using System;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace SettlersEngine
{
    internal class InternalBinding<T> : BindingList<T>
    {
        private Boolean m_AllowChange = true;

        [OnDeserialized]
        void OnDeserialized(StreamingContext context)
        {
            m_AllowChange = true;
        }

        public void Protect()
        {
            m_AllowChange = false;
        }

        public void AddInternal(T inItem)
        {
            try
            {
                m_AllowChange = true;

                Add(inItem);
            }
            finally
            {
                m_AllowChange = false;
            }
        }

        public void InsertInternal(Int32 inIndex, T inItem)
        {
            try
            {
                m_AllowChange = true;

                Insert(inIndex, inItem);
            }
            finally
            {
                m_AllowChange = false;
            }
        }

        public void ClearInternal()
        {
            try
            {
                m_AllowChange = true;

                Clear();
            }
            finally
            {
                m_AllowChange = false;
            }
        }

        public Boolean RemoveInternal(T inItem)
        {
            try
            {
                m_AllowChange = true;

                return Remove(inItem);
            }
            finally
            {
                m_AllowChange = false;
            }
        }

        public void RemoveAtInternal(Int32 inIndex)
        {
            try
            {
                m_AllowChange = true;

                RemoveAt(inIndex);
            }
            finally
            {
                m_AllowChange = false;
            }
        }

        protected override object AddNewCore()
        {
            if (!m_AllowChange)
                throw new InvalidOperationException("List is in read-only state and can not be modified!");

            return base.AddNewCore();
        }

        protected override void ClearItems()
        {
            if (!m_AllowChange)
                throw new InvalidOperationException("List is in read-only state and can not be modified!");

            base.ClearItems();
        }

        protected override void InsertItem(int index, T item)
        {
            if (!m_AllowChange)
                throw new InvalidOperationException("List is in read-only state and can not be modified!");

            base.InsertItem(index, item);
        }

        protected override void RemoveItem(int index)
        {
            if (!m_AllowChange)
                throw new InvalidOperationException("List is in read-only state and can not be modified!");

            base.RemoveItem(index);
        }
    }
}
